-module(ham_server).

-export([start/0, open/1, is_running/1]).

-type listener() :: {atom(), inet:port_number(), [esockd:option()]}. 

%%------------------------------------------------------------------------------
%% @doc
%% Start emqttd application.
%%
%% @end
%%------------------------------------------------------------------------------
-spec start() -> ok | {error, any()}.
start() ->
    application:start(ham_server).

-spec open([listener()] | listener()) -> any().
open(Listeners) when is_list(Listeners) ->
    [open(Listener) || Listener <- Listeners];

%% open http port
open({http, Port, Options}) ->
    MFArgs = {ham_server_http, handle, []},
	  mochiweb:start_http(Port, Options, MFArgs);

open({https, Port, Options}) ->
    MFArgs = {ham_server_http, handle, []},
    mochiweb:start_http(Port, Options, MFArgs).

is_running(Node) ->
    case rpc:call(Node, erlang, whereis, [ham_server]) of
        {badrpc, _}          -> false;
        undefined            -> false;
        Pid when is_pid(Pid) -> true
    end.
